package org.song.util;


import org.song.exception.myException.CommonException;
import tk.mybatis.mapper.additional.insert.InsertListMapper;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface MapperUtil<T> extends Mapper<T>, InsertListMapper<T>, ConditionMapper<T>, IdsMapper<T> {

    default String makeKey() {
        return SnowflakeUtil.getNextId();
    }

    default Long makeLongKey() {
        return SnowflakeUtil.getNextLongId();
    }

    default List<String> makeKeys(int n) {
        return SnowflakeUtil.getNextIds(n);
    }

    /**
     * 乐观锁更新方法
     * <p>
     * 表中加@Version 字段
     *
     * @param t
     * @return
     */
    default int updateByPrimaryKeyWithVersion(T t) {
        int result = updateByPrimaryKey(t);
        if (result == 0) {
            throw new CommonException();
        }
        return result;
    }
}
